package com.github.rabbithome.pipline;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.selector.Selectable;

import java.io.BufferedWriter;
import java.io.IOException;
import java.time.Month;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * Created by tomalloc on 17-1-6.
 */
public class WubaCSVPipeline implements Pipeline {
    private BufferedWriter bufferedWriter;

    private String publishDateReg = "\\d{2}-\\d{2}";

    public WubaCSVPipeline(BufferedWriter bufferedWriter) {
        System.out.println("bufferedWriter=" + bufferedWriter);
        this.bufferedWriter = bufferedWriter;
        StringBuilder stringBuilder = new StringBuilder();
        append("标题", stringBuilder);
        append("链接地址", stringBuilder);
        append("出租方式", stringBuilder);
        append("房屋类型", stringBuilder);
        append("面积", stringBuilder);
        append("楼层", stringBuilder);
        append("地址", stringBuilder);
        append("街道", stringBuilder);
        append("来源", stringBuilder);
        append("价格", stringBuilder);
        stringBuilder.append("发布时间");
        try {
            bufferedWriter.write(stringBuilder.toString());
            bufferedWriter.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void append(String value, StringBuilder stringBuilder) {
        stringBuilder.append("\"");
        stringBuilder.append(value);
        stringBuilder.append("\"");
        stringBuilder.append(",");
    }

    private boolean isSkip(ResultItems resultItems, String source, String price, String time, String size) {
        if (!source.contains("个人")) {
            return true;
        }
        size = size.replace("㎡", "").trim();
        double area = Double.parseDouble(size);
        if (area < 40) {
            return true;
        }
        return false;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        List<Selectable> selectableList = resultItems.get("data");
        if (selectableList == null) {
            return;
        }
        int skipNum = 0;
        for (Selectable selectable : selectableList) {
            Selectable desSelectable = selectable.css("div.des");

            String title = desSelectable.$("h2>a", "text").get();
            String href = desSelectable.$("h2>a", "href").get();
            String rentWay = "";
            String zoom = desSelectable.xpath("//p[@class='room']/text()").get();
            String[] zooms = zoom.split("\\s+");
            String houseType = zoom;
            String size = "";
            if (zooms.length == 2) {
                houseType = zooms[0];
                size = zooms[1].replaceAll("\\u00A0", "");
            }
            String floor = "";
            String address = desSelectable.xpath("//p[@class='add']/allText()").get();
            String source = desSelectable.xpath("//p[@class='geren']/allText()").get();
            Selectable listlirightSelectable = selectable.css("div.listliright");

            String price = listlirightSelectable.css("div.money>b", "text").get();
            String time = listlirightSelectable.css("div.sendTime", "text").get();
            if (time.matches(publishDateReg)) {
                MonthDay monthDay = MonthDay.parse(time, DateTimeFormatter.ofPattern("MM-dd"));
                if (monthDay.getMonth() != Month.JANUARY) {
                    skipNum++;
                    continue;
                }
            }
            if (isSkip(resultItems, source, price, time, size)) {
                continue;
            }

            StringBuilder stringBuilder = new StringBuilder();
            append(title, stringBuilder);
            append(href, stringBuilder);
            append(rentWay, stringBuilder);
            append(houseType, stringBuilder);
            append(size, stringBuilder);
            append(floor, stringBuilder);
            append(address, stringBuilder);
            String[] addr = address.split("\\s+");
            String street = addr[1].trim();
            append(street, stringBuilder);
            append(source, stringBuilder);
            append(price, stringBuilder);
            stringBuilder.append(time);
            try {
                bufferedWriter.write(stringBuilder.toString());
                bufferedWriter.newLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (skipNum == selectableList.size()) {
            resultItems.setSkip(true);
        }

    }

    public static void main(String[] args) {
        String s = "    78".replaceAll("\\u00A0", "");
        System.out.println((int) s.charAt(0));
        System.out.println("s=" + StringUtils.strip(s));
        System.out.println(Integer.toHexString(160));
    }
}
